<?php

namespace app\common\model;

use think\Model;

class MessagePush extends Model {

    // 指定表名,不含前缀
    protected $name = 'message_push';

    /**
     * 根据一个userid数组 和 要插入的数据批量
     */
    public function insertAllByUserids($userids, $data) {
        if (is_null($data))
            return false;
        $datas = array();
        foreach ($userids as $key => $val) {
            $data['identity_id'] = $val['id'];
            $data['acc_name'] = $val['username'];
            $datas[] = $data;
        }
		@$this->insertAll($datas);
        return 1;
    }

    public function importUserById($data) {

        if (empty($data))
            return false;


        $sql = "insert into ex_" . $this->name . "(type,acc_name,content,title,oper_time,operator,identity_id,admin_reg,plaform_id) ";
        $sql .= "select '{$data['type']}',u.username,'{$data['content']}','{$data['title']}','{$data['oper_time']}','{$data['operator']}',uc.id,'{$data['admin_reg']}','{$data['plaform_id']}'";
        $cont = ' u.userid>0';
        switch ($data['importmode']) {
            case '4':
                $cont = ' u.specialty_id=' . $data['bid']; //专业
                break;
            case '3':
                $cont = ' u.project_id=' . $data['bid']; //项目
                break;
            case '9':

                $cont = ' uc.userid in(' . implode(',', $data['userid']) . ')'; //项目
                break;
        }
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        if (!empty($child_plaform)){
            $cplaform ='1';
            if (count($child_plaform)>1){
                foreach ($child_plaform as $kc=>$kv)
                {
                    $cplaform .=",'".$kv."' ";
                }
            }else{
                $cplaform = implode(',',$child_plaform);
                $cplaform = "'".$cplaform."'";
            }
        }else{
            $cplaform = "'".$data['admin_reg']."'";
        }


        $sql .= " from ex_user as u left join ex_user_company as uc on u.userid=uc.userid where uc.admin_reg in ({$cplaform}) and uc.plaform_id='{$data['plaform_id']}' and uc.status=0 and " . $cont;

        $this->execute($sql);
        return true;
    }

}
